package edu.whut.mall.user.controller;



import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import edu.whut.mall.common.constant.FileConstant;
import edu.whut.mall.common.redirect.HttpUtil;
import edu.whut.mall.common.enums.FileType;
import edu.whut.mall.pojo.mapper.FileMapper;
import edu.whut.mall.pojo.model.File;
import io.swagger.annotations.ApiOperation;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-28 09:01
 * Description: 文件上传重定向,避免接收端的上传工程
 */
@ApiIgnore
@RestController
@RequestMapping(value = "/file/redirect")
public class RedirectController {

    @Resource
    private FastFileStorageClient fastFileStorageClient;

    @Resource
    private FileMapper fileMapper;

    public static String getFileFullPath(String fileStorePath) {
        return FileConstant.fastDfsHost + fileStorePath;
    }


    @ApiOperation(value = "重定向", hidden = true)
    @RequestMapping(value = "/**", method = RequestMethod.POST)
    public void redirect(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
        // 获取重定向地址
        String url = request.getRequestURL().toString();
        String newUrl = url.replace("file/redirect/", "");
        List<NameValuePair> additionalMap = new ArrayList<>();
        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String fileName = fileNames.next();//file
            List<MultipartFile> files = request.getFiles(fileName);
            for (MultipartFile file : files) {
                if (file != null && file.getSize() > 0) {
                    String originalFilename = file.getOriginalFilename();
                    String fileExtName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                    StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), fileExtName, null);
                    String relativeAddress = storePath.getFullPath();
                    String fileUrl = getFileFullPath(relativeAddress);
                    if (fileUrl.startsWith("https://47.96.156.152")) {
                        url = url.replace("https://47.96.156.152", "http://d9labmall.top");
                    }
                    File fileNew = new File(fileUrl, new Date(), false, FileType.图片);
                    fileMapper.insert(fileNew);
                    String fileThumUrl = fileUrl.replace("." + fileExtName, "_100x100." + fileExtName);
                    File fileNewThum = new File(fileThumUrl, new Date(), false, FileType.图片);
                    fileMapper.insert(fileNewThum);
                    additionalMap.add(new BasicNameValuePair(fileName, fileUrl));
                    additionalMap.add(new BasicNameValuePair(fileName + "Thumb", fileThumUrl));
                } else {
                    additionalMap.add(new BasicNameValuePair(fileName, ""));
                }
            }
        }
        HttpUtil.httpRequest(newUrl, request, response, additionalMap);//转发
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}